package com.poc.train.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.HibernateDao;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.poc.train.entity.PocUser;

/**
 * 业务持久层
 * 
 * @author Andy
 *
 */
@Repository
public class PocUserDao extends HibernateDao {

	public void pageUser(Page<PocUser> page, Map<String, Object> parameter)
			throws Exception {

		StringBuilder sb = new StringBuilder(" from " + PocUser.class.getName());

		Map<String, Object> args = null;

		if (null != parameter && !parameter.isEmpty()) {
			args = new HashMap<String, Object>();
			sb.append(" where 1=1 ");

			Object obj = parameter.get("ageMin");
			if (null != obj) {
				sb.append(" and age>=:ageMin ");
				args.put("ageMin", obj);
			}

			obj = parameter.get("ageMax");
			if (null != obj) {
				sb.append(" and age<=:ageMax ");
				args.put("ageMax", obj);
			}
			
			obj = parameter.get("deptId");
			if (null != obj) {
				sb.append(" and deptId=:deptId ");
				args.put("deptId", obj);
			}
		}

		String countHql = "select count(*) " + sb;

		this.pagingQuery(page, sb.toString(), countHql, args);

	}

	public void saveData(Collection<PocUser> datas) {
		Session session = this.getSessionFactory().openSession();
		try {
			EntityState state = EntityState.NONE;
			for (PocUser user : datas) {
				state = EntityUtils.getState(user);
				if (state.equals(EntityState.NEW)) {

					user.setUserId(UUID.randomUUID().toString());
					session.save(user);

				} else if (state.equals(EntityState.MODIFIED)) {
					session.update(user);

				} else if (state.equals(EntityState.DELETED)) {
					session.delete(user);
				}
			}
		} finally {
			session.flush();
			session.close();
		}
	}

	public void addData(PocUser user) {
		Session session = this.getSessionFactory().openSession();
		try {
			session.save(user);
		} finally {
			session.flush();
			session.close();
		}
	}

	public List<PocUser> listUser(Map<String, Object> parameter) {
		StringBuffer sb = new StringBuffer();
		sb.append(" from " + PocUser.class.getName());
		sb.append(" where 1=1 ");

		Object obj = parameter.get("positionId");
		if (null != obj) {
			sb.append(" and positionId=:positionId ");
		}

		obj = parameter.get("deptId");
		if (null != obj) {
			sb.append(" and deptId=:deptId ");
		}

		return this.query(sb.toString(), parameter);
	}

	/**
	 * 操作子表数据，同样根据状态操作
	 * @param users
	 * @param session
	 */
	public void saveUsers(List<PocUser> users, Session session) {
		if (null == users || users.isEmpty()) {
			return;
		}
		EntityState state = null;
		for (PocUser user : users) {
			state = EntityUtils.getState(user);
			if (EntityState.NEW.equals(state)) {
				// 新增
				session.save(user);
			}
			if (EntityState.MODIFIED.equals(state)) {
				// 修改
				session.update(user);
			}
			if (EntityState.DELETED.equals(state)) {
				// 删除
				session.delete(user);
			}
		}
	}

	/**
	 * 根据岗位ID删除用户
	 * @param positionId
	 */
	public void delUserByPositionId(String positionId, Session session) {
		String hql = " delete from "+PocUser.class.getName()+
				" where positionId=:positionId";
		
		Query q = session.createQuery(hql);
		q.setString("positionId", positionId);
		q.executeUpdate();
	}

	

}
